package cn.itcast.bos.web.action.take_delivery;

import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.bos.service.take_delivery.OrderService;
import cn.itcast.bos.web.action.common.BaseAction;
import com.alibaba.fastjson.JSONArray;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@Namespace("/")
@ParentPackage("struts-default")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class OrderAction extends BaseAction<Order> {

    @Autowired
    private OrderService orderService;

    @Action(value = "order_findByNum")
    public void findByNum() {
        Map<String, Object> mapData = new HashMap<>();
        Order order = orderService.findByNum(model.getOrderNum());
        if (order != null) {
            mapData.put("success", true);
            mapData.put("orderData", order);
        } else {
            mapData.put("success", false);
        }
        String json = JSONArray.toJSONString(mapData);
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        try {
            ServletActionContext.getResponse().getWriter().write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
